package com.gs.spider.tq;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Encryption {
	
	public static String bytes2Hex(byte[] bys) { 
		char[] HEX = "0123456789abcdef".toCharArray();
        char[] chs = new char[bys.length * 2];  
        for(int i = 0, offset = 0; i < bys.length; i++) {  
            chs[offset++] = HEX[bys[i] >> 4 & 0xf];  
            chs[offset++] = HEX[bys[i] & 0xf];  
        }  
        return new String(chs);  
    }  
	
	public static String hamcMd5(String key,String data)
	{
		SecretKey sk=new SecretKeySpec(key.getBytes(),"HmacMD5");
		Mac mac = null;
		try {
			mac = Mac.getInstance(sk.getAlgorithm());
			mac.init(sk);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bytes2Hex(mac.doFinal(data.getBytes()));
	}

	public static void main(String[] args){
		System.out.println(Encryption.bytes2Hex("xxfxxf".getBytes()));
	}
	
}
